package com.student.servlet;



        import com.student.dao.StudentDao;
        import com.student.model.Student;

        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import java.io.IOException;
        import java.text.ParseException;
        import java.text.SimpleDateFormat;
        import java.util.Date;

/**
 * @author 康娜的小肥腿
 */
@WebServlet(value = {"/servlet/addEmployee"})
public class AddStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Student emp = new Student();
        try {
            String hire_date = request.getParameter("hire_date"); // yyyy-MM-dd
            if (hire_date != null) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date hiredate = format.parse(hire_date);
                emp.setHiredate(hiredate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        boolean successful = new StudentDao().save(emp);
        if (successful) {
            // 只要是做insert,update,delete操作完成后都是使重定向
            response.sendRedirect(request.getContextPath() + "/1.jsp");
        } else {
            response.sendRedirect(request.getContextPath() + "/addForm.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
